package com.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dao.cargo.ContractDao;
import com.dao.cargo.ExtCproductDao;
import com.domain.cargo.Contract;
import com.domain.cargo.ExtCproduct;
import com.domain.cargo.ExtCproductExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.cargo.ExtCproductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public PageInfo findAll(ExtCproductExample example, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    //附件新增,需要同步更新合同表的数量和金额
    public Integer save(ExtCproduct contractProduct) {
        contractProduct.setId(UUID.randomUUID().toString());
        Double amount = getAmount(contractProduct);
        contractProduct.setAmount(amount);

        //设置合同的数量和金额
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);//原合同对象
        contract.setExtNum(contract.getExtNum() + contractProduct.getCnumber());
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);
        //新增附件
        return extCproductDao.insertSelective(contractProduct);
    }

    @Override
    //更新附件,更新后合同数据 = 原合同 - 原附件 + 现
    public Integer update(ExtCproduct contractProduct) {
        String id = contractProduct.getId();

        Double amount = getAmount(contractProduct);
        contractProduct.setAmount(amount);

        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());//原合同对象
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);//原附加对象

        //设置合同的更新数据
        contract.setExtNum(contract.getExtNum() - extCproduct.getCnumber() + contractProduct.getCnumber());
        contract.setTotalAmount(contract.getTotalAmount() - extCproduct.getAmount() + amount);

        contractDao.updateByPrimaryKeySelective(contract);
        return extCproductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public Integer delete(String id) {
        //获取原附件对象
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);

        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());//原合同
        contract.setExtNum(contract.getExtNum() - extCproduct.getCnumber());
        contract.setTotalAmount(contract.getTotalAmount() - extCproduct.getAmount());
        contractDao.updateByPrimaryKeySelective(contract);
        return extCproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    private Double getAmount(ExtCproduct extCproduct){
        Integer cnumber = extCproduct.getCnumber();
        Double price = extCproduct.getPrice();
        double amount = 0.0d;
        if (cnumber != null && price != null) {
            amount = cnumber * price;
        }
        return amount;
    }
}
